package com.yxzx.ebs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxzx.common.YxzxResult;
import com.yxzx.common.EduException;
import com.yxzx.ebs.entity.EbsTeacher;
import com.yxzx.ebs.entity.vo.TeacherQuery;
import com.yxzx.ebs.service.EbsTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-22
 */
@Api(tags = "讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EbsTeacherController {

    @Autowired
    private EbsTeacherService teacherService;

    //6.根据id修改讲师
    @ApiOperation("根据id修改讲师")
    @PostMapping("updateById")
    public YxzxResult updateById(
            @ApiParam(name = "teacher",value = "讲师对象",required = true)
            @RequestBody EbsTeacher teacher){
        if (teacherService.updateById(teacher)){
            return YxzxResult.ok();
        } else {
            return YxzxResult.error();
        }
    }


    //5.根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("/getById/{id}")
    public YxzxResult getById(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        EbsTeacher teacher = teacherService.getById(id);
        try {
            //System.out.println(5/0);//测试全局异常处理
        }catch (Exception e){
            //抛出自定义异常
            throw new EduException(20001,"执行了自定义异常");
        }

        return YxzxResult.ok().data("item",teacher);
    }


    //4.添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public YxzxResult addTeacher(@ApiParam(name = "teacher",value = "讲师对象",required = true) @RequestBody EbsTeacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return YxzxResult.ok();
        }else {
            return YxzxResult.error();
        }
    }

    //3.分页查询讲师列表
    @ApiOperation(value = "分页查询讲师")
    @PostMapping("pageList/{page}/{limit}")
    public YxzxResult getPageList(@ApiParam(name = "page",value = "当前页",required = true) @PathVariable Long page,
                                  @ApiParam(name = "limit",value = "每页大小",required = true) @PathVariable Long limit,
                                  @ApiParam(name = "teacherQuery",value = "查询对象",required = false) @RequestBody TeacherQuery teacherQuery){
//        IPage<EbsTeacher> teacherIPage = teacherService.page(new Page<>(page, limit), null);
        Page<EbsTeacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam,teacherQuery);
        return YxzxResult.ok().data("total",pageParam.getTotal())
                              .data("items",pageParam.getRecords());
    }

    //2.删除讲师
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public YxzxResult removeById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean remove = teacherService.removeById(id);
        if (remove){
            return YxzxResult.ok().message("讲师删除成功");
        }else{
            return YxzxResult.ok().message("讲师删除失败");
        }
    }

    //1.查询所有的讲师功能
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping
    public YxzxResult getAllTeacher(){
        List<EbsTeacher> list = teacherService.list(null);
        return YxzxResult.ok().data("items",list);
    }
}

